package com.lee.common.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.lee.common.exception.FailureStatus;

/**
 * 响应返回对象
 *
 * @author Lee HN
 * @date 2020/7/18 17:55
 */
@JsonInclude(Include.NON_NULL)
public class ResultVO<T> {
    // 状态码
    public final int code;
    // 返回信息
    public final String msg;
    // 返回实体结果
    public final T data;

    private static final int OK = 1;

    private static final ResultVO<Void> SUCCESS = new ResultVO<>(null);

    public static ResultVO<Void> success() {
        return SUCCESS;
    }

    public static <T> ResultVO<T> success(T data) {
        return new ResultVO<>(data);
    }

    public static ResultVO<Void> failure(FailureStatus status) {
        return failure(status, status.getMessage());
    }

    public static ResultVO<Void> failure(FailureStatus status, String msg) {
        return new ResultVO<>(status.getCode(), msg, null);
    }

    private ResultVO(T data) {
        this(OK, null, data);
    }

    private ResultVO(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

}
